#
# clhep_set_compiler_flags() 
#    sets the default compiler flags
#    calls clhep_autoconf_variables
#
# clhep_autoconf_variables() defines global variables
#
# clhep_package_config():
#    defines ${PACKAGE}_LIBS
#    processes ${PACKAGE}-config.in
#    processes ${PACKAGE}-deps.in
#
# clhep_package_config_no_lib():
#    processes ${PACKAGE}-config.in
#
# clhep_config():
#    processes clhep-config.in
#
# clhep_lib_suffix();
#    check for -DLIB_SUFFIX=xxx and process intelligently
#

# CLHEP_SINGLE_THREAD is OFF (false) by default
option(CLHEP_SINGLE_THREAD "Build without multithreading" OFF)

# CLHEP_BUILD_DOCS is OFF (false) by default
option(CLHEP_BUILD_DOCS "Build and install CLHEP documentation" OFF)

# CLHEP_BUILD_STATIC_LIBS is ON (true) by default
option(CLHEP_BUILD_STATIC_LIBS "Build and install CLHEP static libraries" ON)

macro( clhep_check_variable_names )
  # useful if you need to check a variable
  message( STATUS "clhep_check_variable_names: CMAKE_SYSTEM_NAME is ${CMAKE_SYSTEM_NAME}" )
  message( STATUS "clhep_check_variable_names: CMAKE_BASE_NAME is ${CMAKE_BASE_NAME}" )
  message( STATUS "clhep_check_variable_names: CMAKE_CXX_COMPILER_ID is ${CMAKE_CXX_COMPILER_ID}" )
  message( STATUS "clhep_check_variable_names: CMAKE_COMPILER_IS_GNUCXX is ${CMAKE_COMPILER_IS_GNUCXX}" )
  message( STATUS "clhep_check_variable_names: CMAKE_COMPILER_IS_MINGW is ${CMAKE_COMPILER_IS_MINGW}" )
  message( STATUS "clhep_check_variable_names: CMAKE_COMPILER_IS_CYGWIN is ${CMAKE_COMPILER_IS_CYGWIN}" )
  message( STATUS "clhep_check_variable_names: CMAKE_AR is ${CMAKE_AR}" )
  message( STATUS "clhep_check_variable_names: CMAKE_RANLIB is ${CMAKE_RANLIB}" )
  message( STATUS "clhep_check_variable_names: CMAKE_CXX_OUTPUT_EXTENSION is ${CMAKE_CXX_OUTPUT_EXTENSION}" )
  message( STATUS "clhep_check_variable_names: CMAKE_SHARED_LIBRARY_CXX_FLAGS is ${CMAKE_SHARED_LIBRARY_CXX_FLAGS}" )
  message( STATUS "clhep_check_variable_names: CMAKE_SHARED_MODULE_CXX_FLAGS is ${CMAKE_SHARED_MODULE_CXX_FLAGS}" )
  message( STATUS "clhep_check_variable_names: CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS is ${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS}" )
  message( STATUS "clhep_check_variable_names: CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}" )
  message( STATUS "clhep_check_variable_names: CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}" )
  message( STATUS "clhep_check_variable_names: CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}" )
  message( STATUS "clhep_check_variable_names: CMAKE_CXX_FLAGS_RELWITHDEBINFO is ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}" )
  message( STATUS "clhep_check_variable_names: CMAKE_CXX_STANDARD_LIBRARIES is ${CMAKE_CXX_STANDARD_LIBRARIES}" )
  message( STATUS "clhep_check_variable_names: CMAKE_CXX_LINK_FLAGS is ${CMAKE_CXX_LINK_FLAGS}" )
  message( STATUS "clhep_check_variable_names: CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS is ${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS}" )
  message( STATUS "clhep_check_variable_names: CMAKE_SHARED_LINKER_FLAGS is ${CMAKE_SHARED_LINKER_FLAGS}" )
endmacro( clhep_check_variable_names )

macro( clhep_autoconf_variables )

  # these variables are used by <package>-config.in
  # typical values from autoconf:
  #   CXXFLAGS = -g -O2
  #   CXX = g++
  #   CXXCPP = g++ -E
  #   CPPFLAGS = 
  #   CXXLD = $(CXX)
  #   LDFLAGS = 
  #   LIBS = 

  # automake/autoconf variables
  set( prefix      ${CMAKE_INSTALL_PREFIX} )
  set( exec_prefix "\${prefix}" )
  set( includedir  "\${prefix}/include" )
  set( configure_input "Generated by cmake" )

  ### autoconf variables typically do not have the path
  set( CXX ${CMAKE_BASE_NAME} )
  set( CXXCPP       "${CXX} -E" )
  if( ${CMAKE_BUILD_TYPE} MATCHES "Debug" )
     set( CXXFLAGS  ${CMAKE_CXX_FLAGS_DEBUG} )
  elseif( ${CMAKE_BUILD_TYPE} MATCHES "Release" )
     set( CXXFLAGS  ${CMAKE_CXX_FLAGS_RELEASE} )
  elseif( ${CMAKE_BUILD_TYPE} MATCHES "RelWithDebInfo" )
     set( CXXFLAGS  ${CMAKE_CXX_FLAGS_RELWITHDEBINFO} )
  elseif( ${CMAKE_BUILD_TYPE} MATCHES "MinSizeRel" )
     set( CXXFLAGS  ${CMAKE_CXX_FLAGS_MINSIZEREL} )
  endif()
  ##message( STATUS "build type ${CMAKE_BUILD_TYPE} has ${CXXFLAGS}")
  set( LDFLAGS      ${CMAKE_MODULE_LINKER_FLAGS} )
  set( LIBS         "" )
  set( DIFF_Q       "diff -q -b" )

  if( CLHEP_DEBUG_MESSAGES )
    clhep_check_variable_names()
  endif()

endmacro( clhep_autoconf_variables )

macro( _clhep_verify_cxx11 )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.5 )
      message( FATAL_ERROR "CLHEP now requires c++11 support with clang 3.5 or later")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0 )
      message( FATAL_ERROR "CLHEP now requires c++11 support with Apple Clang 6.0 or later")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14.0)
      message( FATAL_ERROR "c++11 extension is not available for ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8 )
      message( FATAL_ERROR "CLHEP now requires c++11 support with gcc 4.8 or later")
    else()
      set( HAVE_STDCXX true )
    endif()
  else()
    message(STATUS "clhep_set_compiler_flags: Do not know how to set c++11 extensions for ${CMAKE_CXX_COMPILER_ID}")
  endif()
endmacro( _clhep_verify_cxx11 )

macro( _clhep_verify_cxx1y )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.5 )
      message( FATAL_ERROR "c++1y extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0 )
      message( FATAL_ERROR "c++1y extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 15.0)
      message( FATAL_ERROR "c++11 extension is not available for ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9 )
      message( FATAL_ERROR "c++1y extension is not available for ${CMAKE_CXX_COMPILER}")
    else()
      set( HAVE_STDCXX true )
    endif()
  else()
    message(STATUS "clhep_set_compiler_flags: Do not know how to set c++1y extensions for ${CMAKE_CXX_COMPILER_ID}")
  endif()
endmacro( _clhep_verify_cxx1y )

macro( _clhep_verify_cxx14 )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.5 )
      message( FATAL_ERROR "c++14 extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0 )
      message( FATAL_ERROR "c++14 extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16.0)
      message( FATAL_ERROR "c++14 extension is not available for ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9 )
      message( FATAL_ERROR "c++14 extension is not available for ${CMAKE_CXX_COMPILER}")
    else()
      set( HAVE_STDCXX true )
    endif()
  else()
    message(STATUS "clhep_set_compiler_flags: Do not know how to set c++14 extensions for ${CMAKE_CXX_COMPILER_ID}")
  endif()
endmacro( _clhep_verify_cxx14 )

macro( _clhep_verify_cxx1z )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.0 )
      message( FATAL_ERROR "c++1z extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0 )
      message( FATAL_ERROR "c++1z extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17.0)
      message( FATAL_ERROR "c++1z extension is not available for ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0 )
      message( FATAL_ERROR "c++1z extension is not available for ${CMAKE_CXX_COMPILER}")
    else()
      set( HAVE_STDCXX true )
    endif()
  else()
    message(STATUS "clhep_set_compiler_flags: Do not know how to set c++1z extensions for ${CMAKE_CXX_COMPILER_ID}")
  endif()
endmacro( _clhep_verify_cxx1z )

macro( _clhep_verify_cxx17 )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0 )
      message( FATAL_ERROR "c++17 extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0 )
      message( FATAL_ERROR "c++17 extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18.0)
      message( FATAL_ERROR "c++17 extension is not available for ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.4.0 )
      message( FATAL_ERROR "c++17 extension is not available for ${CMAKE_CXX_COMPILER}")
    else()
      set( HAVE_STDCXX true )
    endif()
  else()
    message(STATUS "clhep_set_compiler_flags: Do not know how to set c++17 extensions for ${CMAKE_CXX_COMPILER_ID}")
  endif()
endmacro( _clhep_verify_cxx17 )

macro( _clhep_verify_cxx2a )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0 )
      message( FATAL_ERROR "c++2a extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0 )
      message( FATAL_ERROR "c++2a extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 2021.1)
      message( FATAL_ERROR "c++2a extension is not available for ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.1.0 )
      message( FATAL_ERROR "c++2a extension is not available for ${CMAKE_CXX_COMPILER}")
    else()
      set( HAVE_STDCXX true )
    endif()
  else()
    message(STATUS "clhep_set_compiler_flags: Do not know how to set c++2a extensions for ${CMAKE_CXX_COMPILER_ID}")
  endif()
endmacro( _clhep_verify_cxx2a )

macro( _clhep_verify_cxx20 )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0 )
      message( FATAL_ERROR "c++20 extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 13.0 )
      message( FATAL_ERROR "c++20 extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 2021.1)
      message( FATAL_ERROR "c++20 extension is not available for ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.1.0 )
      message( FATAL_ERROR "c++20 extension is not available for ${CMAKE_CXX_COMPILER}")
    else()
      set( HAVE_STDCXX true )
    endif()
  else()
    message(STATUS "clhep_set_compiler_flags: Do not know how to set c++20 extensions for ${CMAKE_CXX_COMPILER_ID}")
  endif()
endmacro( _clhep_verify_cxx20 )

macro( _clhep_verify_cxx23 )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17.0 )
      message( FATAL_ERROR "c++23 extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16.0 )
      message( FATAL_ERROR "c++23 extension is not available for ${CMAKE_CXX_COMPILER_ID}${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 2022.3)
      message( FATAL_ERROR "c++23 extension is not available for ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
    else()
      set( HAVE_STDCXX true )
    endif()
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0.0 )
      message( FATAL_ERROR "c++23 extension is not available for ${CMAKE_CXX_COMPILER}")
    else()
      set( HAVE_STDCXX true )
    endif()
  else()
    message(STATUS "clhep_set_compiler_flags: Do not know how to set c++23 extensions for ${CMAKE_CXX_COMPILER_ID}")
  endif()
endmacro( _clhep_verify_cxx23 )

macro( _clhep_check_cxxstd )
  if( CLHEP_DEBUG_MESSAGES )
    message(STATUS "_clhep_check_cxxstd debug: CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")
    message(STATUS "_clhep_check_cxxstd debug: CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
    message(STATUS "_clhep_check_cxxstd debug: CMAKE_CXX_COMPILER_VERSION: ${CMAKE_CXX_COMPILER_VERSION}")
    message(STATUS "_clhep_check_cxxstd debug: CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    message(STATUS "_clhep_check_cxxstd debug: CLHEP_BUILD_CXXSTD: ${CLHEP_BUILD_CXXSTD}")
  endif( CLHEP_DEBUG_MESSAGES )
  set( HAVE_STDCXX )
  if( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++0x" )
    message( FATAL_ERROR "The c++0x extension is no longer supported.  Please use c++11 or later.")
  elseif( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++11" )
    _clhep_verify_cxx11()
  elseif( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++1y" )
    _clhep_verify_cxx1y()
  elseif( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++14" )
    _clhep_verify_cxx14()
  elseif( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++1z" )
    _clhep_verify_cxx1z()
  elseif( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++17" )
    _clhep_verify_cxx17()
  elseif( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++2a" )
    _clhep_verify_cxx2a()
  elseif( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++20" )
    _clhep_verify_cxx20()
  elseif( "${CLHEP_BUILD_CXXSTD}" STREQUAL "-std=c++23" )
    _clhep_verify_cxx23()
  elseif( DEFINED CLHEP_BUILD_CXXSTD )
    message( FATAL_ERROR "${CLHEP_BUILD_CXXSTD} is not supported. Supported extensions are c++11, c++1y, c++14, c++1z, c++17, c++2a, c++20 and c++23.")
  else()
    # presume -std=c++11
    set(CLHEP_BUILD_CXXSTD "-std=c++11")
    _clhep_verify_cxx11( )
  endif()
  ##message(STATUS "_clhep_check_cxxstd debug: CLHEP_BUILD_CXXSTD HAVE_STDCXX: ${CLHEP_BUILD_CXXSTD} ${HAVE_STDCXX}")
  if( DEFINED HAVE_STDCXX )
    if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
        set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLHEP_BUILD_CXXSTD}" )
        if (${CMAKE_CXX_FLAGS} MATCHES "-fmodules")
          set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmodule-map-file=${CLHEP_BINARY_DIR}/clhep.modulemap")
        endif()
    elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
	set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLHEP_BUILD_CXXSTD}" )
    elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
	set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLHEP_BUILD_CXXSTD}" )
    elseif(CMAKE_COMPILER_IS_GNUCXX)
	set( CMAKE_CXX_FLAGS "${CLHEP_BUILD_CXXSTD} ${CMAKE_CXX_FLAGS} -D_GNU_SOURCE" )
    else()
      message(STATUS "clhep_set_compiler_flags: Do not know how to set ${CLHEP_BUILD_CXXSTD} extensions for ${CMAKE_CXX_COMPILER_ID}")
    endif()
  endif()
endmacro( _clhep_check_cxxstd )

macro( _clhep_use_pthread )
    set(CLHEP_THREAD_DEFINITION "#define CLHEP_THREAD_LOCAL thread_local")
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads)
    ##message(STATUS "clhep_set_compiler_flags debug: CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    message(STATUS "cmake build type is ${CMAKE_BUILD_TYPE}")
    if(THREADS_HAVE_PTHREAD_ARG) 
      set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )
    elseif(CMAKE_COMPILER_IS_GNUCXX)
      # needed when building with gcc on OSX
      set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )
    endif()
endmacro( _clhep_use_pthread )

macro( clhep_set_compiler_flags )
  ##message(STATUS "incoming cmake build type is ${CMAKE_BUILD_TYPE}")
  if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "" FORCE)
  endif()
  _clhep_check_cxxstd()
  # find pthread
  if(CLHEP_SINGLE_THREAD)
    set(CLHEP_THREAD_DEFINITION "#define CLHEP_THREAD_LOCAL")
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    _clhep_use_pthread()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0 )
      set(CLHEP_THREAD_DEFINITION "#define CLHEP_THREAD_LOCAL")
    else()
      _clhep_use_pthread()
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    if( CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.5 )
      set(CLHEP_THREAD_DEFINITION "#define CLHEP_THREAD_LOCAL")
    else()
      _clhep_use_pthread()
    endif()
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    _clhep_use_pthread()
  else()
    set(CLHEP_THREAD_DEFINITION "#define CLHEP_THREAD_LOCAL")
  endif()
  if( CLHEP_DEBUG_MESSAGES )
    message(STATUS "enable c++11 extensions: ${CMAKE_CXX_FLAGS}")
  endif()
  if( CLHEP_DEBUG_MESSAGES )
    message(STATUS "clhep_set_compiler_flags debug: CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    message(STATUS "clhep_set_compiler_flags debug: cmake compilers ${CMAKE_CXX_COMPILER} ${CMAKE_C_COMPILER}")
  endif( CLHEP_DEBUG_MESSAGES )
  if( CLHEP_DEBUG_MESSAGES )
    message(STATUS "using compiler ${CMAKE_CXX_COMPILER_ID}")
  endif( CLHEP_DEBUG_MESSAGES )
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    set(CMAKE_C_FLAGS "-O -pedantic -Wall ${CMAKE_C_FLAGS}")
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    set(CMAKE_C_FLAGS "-O -pedantic -Wall ${CMAKE_C_FLAGS}")
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel" )
    set(CMAKE_C_FLAGS "-O -ansi -pedantic -Wall {CMAKE_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "-O -ansi -pedantic -Wall ${CMAKE_CXX_FLAGS}")
  elseif( CMAKE_COMPILER_IS_GNUCC )
    set(CMAKE_C_FLAGS "-O -ansi -pedantic -Wall -D_GNU_SOURCE ${CMAKE_C_FLAGS}")
  endif()
  if( ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" )
    set(CMAKE_CXX_FLAGS "-O -pedantic -Wall ${CMAKE_CXX_FLAGS}")
  elseif( ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang" )
    set(CMAKE_CXX_FLAGS "-O -pedantic -Wall ${CMAKE_CXX_FLAGS}")
  elseif(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "-O -ansi -pedantic -Wall -D_GNU_SOURCE ${CMAKE_CXX_FLAGS}")
  endif()
    message(STATUS "clhep_set_compiler_flags debug: CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
  # linker flags
    if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
      set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-undefined,error")
    else()
      set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
    endif()
  message(STATUS "setting CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}")
  # override flags for Windows cl
  if( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
    ##message( STATUS "system is Windows" )
    ##message( STATUS "CMAKE_BASE_NAME is ${CMAKE_BASE_NAME}" )
    if( ${CMAKE_BASE_NAME} MATCHES "cl" )
      ##message( STATUS "compiler is MSVC" )
      ##message( STATUS "incoming basic compiler flags are ${CMAKE_CXX_FLAGS}")
	  set(CLHEP_THREAD_DEFINITION "#define CLHEP_THREAD_LOCAL thread_local")
	  set(CMAKE_C_FLAGS "/EHsc /nologo /GR /MD /D USING_VISUAL")
      set(CMAKE_CXX_FLAGS "/EHsc /nologo /GR /MD /D USING_VISUAL")
      set(CMAKE_SHARED_LINKER_FLAGS "/nologo")
    endif()
  endif()
  clhep_autoconf_variables()
  message( STATUS "compiling with ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_FLAGS} ${CXXFLAGS}")
endmacro( clhep_set_compiler_flags )

macro( clhep_package_config_no_lib )
  set( ${PACKAGE}_CPPFLAGS "-I${includedir}" )
  set( ${PACKAGE}_LDFLAGS  " " )
  set( ${PACKAGE}_LIBS     " " )
  configure_file ( ${CLHEP_SOURCE_DIR}/${PACKAGE}/${PACKAGE}-config.in
                   ${CLHEP_BINARY_DIR}/${PACKAGE}/${PACKAGE}-config @ONLY )
  ## don't install <package>-config on Windows
  if( NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
    install ( PROGRAMS ${CLHEP_BINARY_DIR}/${PACKAGE}/${PACKAGE}-config
              DESTINATION bin
	    )
  endif()
endmacro( clhep_package_config_no_lib )

macro( clhep_package_config )
  set( ${PACKAGE}_CPPFLAGS "-I${includedir}" )
  set( ${PACKAGE}_LDFLAGS  "-L\${exec_prefix}/lib${LIB_SUFFIX}" )
  set( ${PACKAGE}_LIBS     "-lCLHEP-${PACKAGE}-${VERSION}" )
  if( ${PACKAGE}_DEPS )
     foreach ( dep ${${PACKAGE}_DEPS} )
        message( STATUS "${PACKAGE} depends on ${dep}")
	set( ${PACKAGE}_LIBS     "${${PACKAGE}_LIBS} -lCLHEP-${dep}-${VERSION}" )
     endforeach()
  endif()
  configure_file ( ${CLHEP_SOURCE_DIR}/${PACKAGE}/${PACKAGE}-config.in
                   ${CLHEP_BINARY_DIR}/${PACKAGE}/${PACKAGE}-config @ONLY )
  configure_file ( ${CLHEP_SOURCE_DIR}/${PACKAGE}/${PACKAGE}-deps.in
                   ${CLHEP_BINARY_DIR}/${PACKAGE}/${PACKAGE}-deps @ONLY )
  ## don't install <package>-config on Windows
  if( NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
    install ( PROGRAMS ${CLHEP_BINARY_DIR}/${PACKAGE}/${PACKAGE}-config
              DESTINATION bin
	    )
  endif()
endmacro( clhep_package_config )

macro( clhep_config )
  configure_file ( ${CLHEP_SOURCE_DIR}/clhep-config.in
                   ${CLHEP_BINARY_DIR}/clhep-config @ONLY )
  ## don't install clhep-config on Windows
  if( NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
    install ( PROGRAMS ${CLHEP_BINARY_DIR}/clhep-config
              DESTINATION bin
	    )
  endif()
endmacro( clhep_config )

macro( _clhep_lib_suffix_64 )
  if( APPLE )
    #message(STATUS "checking LIB_SUFFIX ${LIB_SUFFIX} against ${CMAKE_SYSTEM_PROCESSOR} and ${CMAKE_OSX_ARCHITECTURES}")
    # On Mac, we use NAME-ARCH, but ARCH is 'Universal' if more than
    # one arch is built for. Note that falling back to use
    # CMAKE_SYSTEM_PROCESSOR may *not* be 100% reliable.
    list(LENGTH CMAKE_OSX_ARCHITECTURES _number_of_arches)
    if(NOT _number_of_arches)
      # - Default
      if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
        # building for 64bit
      else()
        message(STATUS "WARNING: LIB_SUFFIX ${LIB_SUFFIX} inappropriate for this build")
	set(LIB_SUFFIX "")
      endif()
    elseif(_number_of_arches GREATER 1)
      # - Universal
    else()
      # - Use what the user specified
      if (${CMAKE_OSX_ARCHITECTURES} STREQUAL "x86_64")
        # building for 64bit
      else()
        message(STATUS "WARNING: LIB_SUFFIX ${LIB_SUFFIX} inappropriate for this build")
	set(LIB_SUFFIX "")
      endif()
    endif()
  elseif( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
    #message(STATUS "checking LIB_SUFFIX ${LIB_SUFFIX} against ${CMAKE_SYSTEM_PROCESSOR} ")
    if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
      # building for 64bit
    else()
      message(STATUS "WARNING: LIB_SUFFIX ${LIB_SUFFIX} inappropriate for this build")
      set(LIB_SUFFIX "")
    endif()
  else()
    #message(STATUS "checking LIB_SUFFIX ${LIB_SUFFIX} against ${CMAKE_SYSTEM_PROCESSOR} ")
    if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
      # building for 64bit
    else()
      message(STATUS "WARNING: LIB_SUFFIX ${LIB_SUFFIX} inappropriate for this build")
      set(LIB_SUFFIX "")
    endif()
  endif()
endmacro( _clhep_lib_suffix_64 )

macro( _clhep_lib_suffix_32 )
  if( APPLE )
    #message(STATUS "checking LIB_SUFFIX ${LIB_SUFFIX} against ${CMAKE_SYSTEM_PROCESSOR} and ${CMAKE_OSX_ARCHITECTURES}")
    # On Mac, we use NAME-ARCH, but ARCH is 'Universal' if more than
    # one arch is built for. Note that falling back to use
    # CMAKE_SYSTEM_PROCESSOR may *not* be 100% reliable.
    list(LENGTH CMAKE_OSX_ARCHITECTURES _number_of_arches)
    if(NOT _number_of_arches)
      # - Default
      if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
        # building for 32bit
      else()
        message(STATUS "WARNING: LIB_SUFFIX ${LIB_SUFFIX} inappropriate for this build")
	set(LIB_SUFFIX "")
      endif()
    elseif(_number_of_arches GREATER 1)
      # - Universal
    else()
      # - Use what the user specified
      if (${CMAKE_OSX_ARCHITECTURES} STREQUAL "i386")
        # building for 32bit
      else()
        message(STATUS "WARNING: LIB_SUFFIX ${LIB_SUFFIX} inappropriate for this build")
	set(LIB_SUFFIX "")
      endif()
    endif()
  else()
    #message(STATUS "checking LIB_SUFFIX ${LIB_SUFFIX} against ${CMAKE_SYSTEM_PROCESSOR} ")
    if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
      # building for 32bit
    elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
      # building for 32bit
    else()
      message(STATUS "WARNING: LIB_SUFFIX ${LIB_SUFFIX} inappropriate for this build")
      set(LIB_SUFFIX "")
    endif()
  endif()
endmacro( _clhep_lib_suffix_32 )

macro( clhep_lib_suffix )
  if(LIB_SUFFIX)
    if ( ${LIB_SUFFIX} STREQUAL "64" )
      _clhep_lib_suffix_64()
    elseif( ${LIB_SUFFIX} STREQUAL "32" )
      _clhep_lib_suffix_32()
    endif()
  endif()
  message(STATUS "libraries will be installed in $ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}")
endmacro( clhep_lib_suffix )

macro( clhep_enable_asserts )
  string(REGEX REPLACE "-DNDEBUG" " " CXXFLAGS "${CXXFLAGS}" )
  string(TOUPPER ${CMAKE_BUILD_TYPE} BTYPE_UC )
  string(REGEX REPLACE "-DNDEBUG" " " CMAKE_CXX_FLAGS_${BTYPE_UC} "${CMAKE_CXX_FLAGS_${BTYPE_UC}}" )
endmacro( clhep_enable_asserts )
